package org.yoao.moneymachine.business.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.yoao.crm.struts.form.ProductForm;
import org.yoao.crm.tools.TimeTool;
import org.yoao.moneymachine.bean.Product;
import org.yoao.moneymachine.business.service.IProductService;
import org.yoao.moneymachine.dao.ProductDAO;

/**
 * @author  Weishion.Yi
 * @description 
 * @date 2010-10-5 下午09:12:25
 */
public class ProductServiceImpl implements IProductService {
	private ProductDAO productDAO = new ProductDAO();
	
	@Override
	public ProductForm add(ProductForm form) throws Exception {
		Product product = new Product();
		form.setCreateTime(TimeTool.getSystemTime());
		BeanUtils.copyProperties(product, form);
		if(productDAO.insert(product) > -1){
			form.setInfo("产品添加成功!");
		}else{
			form.setInfo("产品添加失败!");
		}
		return form;
	}

	@Override
	public void delete(String id) throws Exception {
		if(id.endsWith(",")) id = id.substring(0, id.length()-1);
		productDAO.delete(id);
	}

	@Override
	public int getDataCount(Map map) throws Exception {
		return productDAO.getDataCount(map);
	}

	@Override
	public List view(int startRow, int endRow, Map map) throws Exception {
		return productDAO.find(startRow, endRow, map);
	}

	@Override
	public ProductForm getProduct(String id) throws Exception {
		ProductForm form = new ProductForm();
		Product product = productDAO.findById(id);
		BeanUtils.copyProperties(form, product);
		return form;
	}

	@Override
	public ProductForm modify(ProductForm form) throws Exception {
		Product product = new Product();
		form.setCreateTime(TimeTool.getSystemTime());
		BeanUtils.copyProperties(product, form);
		if(productDAO.modify(product) > -1){
			form.setInfo("产品修改成功!");
		}else{
			form.setInfo("产品修改失败!");
		}
		return form;
	}

	@Override
	public ProductForm init(String id) throws Exception {
		Product product = productDAO.findById(id);
		ProductForm form = new ProductForm();
		BeanUtils.copyProperties(form, product);
		return form;
	}
}